﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.DynamicData;
using System.Web;
using System.Web.Routing;

namespace NotAClue.Web.DynamicData
{
    public class PKDynamicDataRoute : DynamicDataRoute
    {

        public PKDynamicDataRoute(string url)
            : base(url)
        {
        }

        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            RouteData routeData = base.GetRouteData(httpContext);

            if (routeData != null)
            {
                // Change any reference to PK1, PK2, etc into the actual PK name 
                FixUpRouteValues(routeData.Values, true /*fromFixedName*/);
            }

            return routeData;
        }

        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {

            // Change any reference to the actual PK name into PK1, PK2, etc... 
            FixUpRouteValues(values, false /*fromFixedName*/);

            return base.GetVirtualPath(requestContext, values);
        }

        private void FixUpRouteValues(RouteValueDictionary routeValues, bool fromFixedName)
        {

            // First, try to get the table from the route values 
            object tableName;
            if (!routeValues.TryGetValue("table", out tableName))
                return;

            MetaTable table;
            if (!Model.TryGetTable((string)tableName, out table))
                return;

            // Go through all the table's PK and make the required adjustment 
            int index = 1;
            foreach (MetaColumn column in table.PrimaryKeyColumns)
            {

                // Assign from and to based on which direction we're fixing up 
                string from, to;
                if (fromFixedName)
                {
                    from = "PK" + index;
                    to = column.Name;
                }
                else
                {
                    from = column.Name;
                    to = "PK" + index;
                }

                // If the route has that value, fix it up 
                object val;
                if (routeValues.TryGetValue(from, out val))
                {
                    routeValues[to] = val;
                    routeValues.Remove(from);
                }

                index++;
            }
        }
    }
}
